﻿using System.Collections.Generic;
using System.Data;
using NHibernate.Type;

namespace Golf.DataLayer
{
    using Core;

    public class GenderEnumType : EnumStringType
    {
        public GenderEnumType()
            : base(typeof(Gender)) { }

        public override void Set(IDbCommand cmd, object value, int index)
        {
            if (value is Gender)
                ((IDataParameter)cmd.Parameters[index]).Value = ((Gender)value).ToString().Substring(0, 1);
            else
                base.Set(cmd, value, index);
        }

        public override object GetInstance(object code)
        {
            string status = code.ToString();
            if (GetMappings().ContainsKey(status))
                return GetMappings()[status];
            else
                return base.GetInstance(code);
        }

        private IDictionary<string, Gender> GetMappings()
        {
            IDictionary<string, Gender> mappings = new Dictionary<string, Gender>();
            mappings.Add("F", Gender.Female);
            mappings.Add("M", Gender.Male);
            return mappings;
        }
    }
}
